package com.example.common.config;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.listener.AbstractListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class NacosConfigListener {
    Logger logger = LoggerFactory.getLogger(NacosConfigListener.class);

    /**
     * Nacos dataId.
     */
    public static final String DATA_ID = "nacos-config-example.properties";

    /**
     * Nacos group.
     */
    public static final String GROUP = "DEFAULT_GROUP";

    @Autowired
    private NacosConfigManager nacosConfigManager;

    @Bean
    public ApplicationRunner runner() {
        return args -> nacosConfigManager.getConfigService().addListener(DATA_ID, GROUP, new AbstractListener() {
            @Override
            public void receiveConfigInfo(String configInfo) {
                logger.info("[Listener] {}", configInfo);
            }
        });
    }
}
